home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 665 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.7 KB  |  55 lines

  1. Path: news.halcyon.com!usenet
  2. From: normanb@halcyon.com (Norm Bryar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Operator Overloading
  5. Date: Fri, 05 Jan 1996 17:52:56 GMT
  6. Organization: Northwest Nexus Inc.
  7. Message-ID: <4cjoh2$sn0@news.halcyon.com>
  8. References: <4chdhb$g64@news.cs.hope.edu> <4chht5$s2h@gold.datalytics.com>
  9. NNTP-Posting-Host: blv-pm3-ip9.halcyon.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Rob Stewart <stew@datalytics.com> wrote:
  13.  
  14. >vnopstal@cs.hope.edu (Michael Van Opstall) wrote:
  15. >>A couple of questions on operator overloading:
  16. >>
  17. >>1. If you overload ==, do you have to overload != as well?
  18. >>
  19. >If you supply the one, someone will code the opposite 
  20. >conditional sometime and expect to be able to use the other.  
  21. >Likewise, if you provide ++(+, *, etc.), you should seriously 
  22. >consider --(-, /, etc.).
  23.  
  24. Bearing in mind, of course, the operator!=() is a freebie.
  25.  
  26. BOOL   myclass::operator!=( const myclass &  toCompare )
  27. {
  28.     return !(*this == toCompare);
  29. }
  30.  
  31. >>2. What is the accepted method for overloading >> and <<? The way I do it
  32. >>is not very cool and not slick at all. Should I be including streams and doing
  33. >>stream manipulation? That makes sense.
  34. >>
  35. >If you're doing streams manipulation, do streams manipulation.  
  36. >Remember, >> and << were (are) bit shift operators.  The 
  37. >iostreams classes changed that behavior.  Was that the right 
  38. >thing to do?  Perhaps for your class bit shifting is the right 
  39. >behavior for these operators.
  40.  
  41. The way I've most often seen it is
  42. class myclass {
  43. friend ostream &   operator<<( ostream &  os,  myclass & anObj );
  44. ...
  45. };
  46.  
  47. ostream &   operator<<( ostream & os,  myclass & onObj )
  48. {
  49.     os  << (int) anObj.m_enum1 << (int) anObj.m_Bool1  ...
  50. }
  51.  
  52.  
  53. Hope this helps.    --Norm
  54.  
  55.